﻿<?php
/**
* @version              $Id: view.html.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package              Joomla
* @subpackage   Poll
* @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license              GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the Poll component
 *
 * @static
 * @package             Joomla
 * @subpackage  Poll
 * @since 1.0
 */
               
 class amapViewgestusers extends JView
{
        var $amap;
        function display($tpl = null)
        {
        $mainframe =& JFactory::getApplication();      
                if($this->amap->adLoggue && $mainframe->getUserState("amap_droitAbo", FALSE))           {
                        echo '<div class="amap_bloc">';
                        echo '<h3 class="amap_titre">Gestion des utilisateurs</h3>';
						
                        $document =& JFactory::getDocument();
                        $pathway  =& $mainframe->getPathway();

                        // Adds parameter handling
                        $params = $mainframe->getParams();

                        //Set page title information
                        $document->setTitle('Gestion des utilisateurs');

                        //Set pathway information
                        $pathway->addItem('Gestion des utilisateurs', '');

                        $params->def( 'show_page_title', 1 );
                        $params->def( 'page_title', 'Utilisateurs');
						
                        //~ Récupération de données 
						$IDJ = JRequest::getVar('IDJ');
						$IDA = JRequest::getVar('IDA');
						$action = JRequest::getVar('action');
						$User =& JFactory::getUser();
						$isroot	= $User->get('isRoot');
						
                        //~ Récupération de données des groupes ("Comité" et "Adhérent")
						$groups = Array("Comité", "Adhérent");
						foreach ($groups as $g)	{
							$query = "SELECT a.title, a.id, b.user_id, b.group_id
										FROM #__usergroups as a
											LEFT JOIN #__user_usergroup_map as b
												ON a.id = b.group_id
										WHERE a.title = '".$g."'";
							$db=&JFactory::getDBO();
							$db->setQuery($query);
							$db->query($query);
							$Liste=$db->loadAssocList("user_id");
							$ListeGroupId[$g] = array_keys($Liste);
							$IdGroup[$g] = $Liste[$ListeGroupId[$g][0]]["group_id"];
						}
						
						switch ($action)	{
							case 'ADD_Comite' :
								gestusers_AjouterMembreComite($IDJ, $IDA, $IdGroup["Comité"]);
								$ListeGroupId["Comité"][] = $IDJ;
								break;
							case 'ADD_Adherent' :
								gestusers_AjouterMembreAdherent($IDJ, $IdGroup["Adhérent"]);
								$ListeGroupId["Adhérent"][] = $IDJ;
								break;
							case 'ADD_Membre' :
								gestusers_ChangerStatutAdherent($IDA, "V");
								break;
							case 'DEL_Membre' :
								gestusers_ChangerStatutAdherent($IDA, "A");
							case 'DEL_Adherent' :
								gestusers_SupprimerMembreAdherent($IDJ, $IdGroup["Adhérent"]);
								foreach ($ListeGroupId["Adhérent"] as $k=>$num)
									if ($num == $IDJ)	unset($ListeGroupId["Adhérent"][$k]);
							case 'DEL_Comite' :
								gestusers_SupprimerMembreComite($IDJ, $IDA, $IdGroup["Comité"]);
								foreach ($ListeGroupId["Comité"] as $k=>$num)
									if ($num == $IDJ)	unset($ListeGroupId["Comité"][$k]);
								break;
							case 'ERA_Membre' :
								if ($isroot) gestusers_SupprimerTout($IDJ, $IDA);
								break;
						}

                        //~ Récupération de la liste des utilisateurs ayant fait une demande d'appartenance ou appartenant à l'AMAP
                        $query="SELECT  a.Nom, a.Prenom, a.IDA, a.IDJ, b.IDA, b.Statut, b.Periode
                                FROM amap_adherent as a
                                        INNER JOIN amap_adhesion as b
                                        ON      a.IDA=b.IDA
                                GROUP BY a.IDA
                                ORDER BY a.Nom,a.Prenom";
                        $db=&JFactory::getDBO();
                        $db->setQuery($query);
                        $db->query($query);
                        $listeadherents=$db->loadAssocList();

						$url =& JURI::getInstance();
						$url->delVar('IDJ');$url->delVar('IDA');$url->delVar('action');$url->delVar('Periode');
						if ($isroot)
                        echo '<div class="amap_alert" style="display: block; height: 70px;">
										<img width="64" height="64" border="0" align="left" src="components/com_amap/images/bullet_error.png" title="ATTENTION !">
                                        <img width="24" height="24" border="0"  src="components/com_amap/images/user_deny.png" title="Suppression de l\'adhérent de l\'AMAP">&nbsp;&nbsp;
										La suppression d\'un compte d\'adhérent est une <b><u style="color: blue;">OPÉRATION IRRÉVERSIBLE</u></b>.
                                </div>';
                        echo '<div align="center">
                                        <img width="24" height="24" border="0"  src="components/com_amap/images/user_tag.png" title="Adhérent de l\'AMAP"> Adhérent à l\'AMAP&nbsp;&nbsp;&nbsp;&nbsp;
                                        <img width="24" height="24" border="0"  src="components/com_amap/images/user_starred.png" title="Adhérent de l\'AMAP"> Membre du comité (NB: il faut gérer les personnes relai)
                                </div>';
                        echo '<table width="90%" align="center" border=1>';
                        echo '<tr style="background-color: #0096D2; color: white;">';
						if ($isroot)	echo '<td style="" align="center" width="25"></td>';
                        echo '<td style="font-weight: bold; color: orange; font-size: 16px;" align="center" width="35%">Compte non validé</td>';
                        echo '<td width="10%">&nbsp;</td>';
                        echo '<td style="font-weight: bold; color: green; font-size: 16px;" align="center" width="35%">Compte validé</td>';
                        echo '<td width="30">&nbsp;</td>';
                        foreach ($listeadherents as $adh)       {
							echo '<tr class="amap_beautableau"'.(($User->id == $adh["IDJ"])?' style="color: red; font-weight: bold;"':'').'>';
							if ($isroot)	echo '<td style="" align="center" width="30">'.(($User->id != $adh["IDJ"])?'<div  class="amap_alert"><a href="'.$url->toString().'&action=ERA_Membre&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img width="24" height="24" border="0" src="components/com_amap/images/user_deny.png" title="Cliquer pour supprimer ce membre de la base de données"></a></div>':'').'</td>';
							if ($adh["Statut"] == "V")      echo '<td>&nbsp;</td><td align="center"><a href="'.$url->toString().'&action=DEL_Membre&Periode='.$adh["Periode"].'&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img src="'.JURI::root().'components/com_amap/images/arrow_left.png" title="Invalider le compte" width="24" style="padding: 5px;"></a></td>';
									echo '<td class="amap_beautableau">'.str_replace("<br>", " - ", FormatNomsAdh($adh["Nom"], $adh["Prenom"])).'</td>';
							if ($adh["Statut"] == "A")      echo '<td align="center"><a href="'.$url->toString().'&action=ADD_Membre&Periode='.$adh["Periode"].'&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img src="'.JURI::root().'components/com_amap/images/arrow_right.png" title="Valider le compte" width="24" style="padding: 5px;"></a></td><td>&nbsp;</td>';
							echo '<td align="center">';
							if ($adh["Statut"] == "V")
								echo ((in_array($adh["IDJ"], $ListeGroupId["Adhérent"]))?'<a href="'.$url->toString().'&action=DEL_Adherent&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img width="24" height="24" border="0"  src="components/com_amap/images/user_tag.png" title="Cliquer pour retirer des adhérents de l\'AMAP"></a>':'<a href="'.$url->toString().'&action=ADD_Adherent&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img width="24" height="24" border="0"  src="components/com_amap/images/user_tagNO.png" title="Cliquer pour ajouter aux adhérents de l\'AMAP"></a>').'&nbsp;&nbsp;';
								if	(in_array($adh["IDJ"], $ListeGroupId["Adhérent"]))	{
									if	(in_array($adh["IDJ"], $ListeGroupId["Comité"]))
										echo '<a href="'.$url->toString().'&action=DEL_Comite&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img width="24" height="24" border="0" src="components/com_amap/images/user_starred.png" title="Cliquer pour retirer des membres du Comité"></a>';
									else
										echo '<a href="'.$url->toString().'&action=ADD_Comite&IDJ='.$adh["IDJ"].'&IDA='.$adh["IDA"].'"><img width="24" height="24" border="0"  src="components/com_amap/images/user_starredNO.png" title="Cliquer pour ajouter aux membres du comité" ></a>';
								}
								else
									echo '<img width="24" height="24" border="0"  src="components/com_amap/images/invisible.png" title="" >';
							echo '</td>';
							echo '</tr>';
                        }
                        echo '</table>';

                }
                else
                        echo 'Problème de droit d\'accès';
                //JRequest::setCmd( 'layout', $template );              
                parent::display($tpl);
        }
}

function gestusers_SupprimerTout ($IDJ, $IDA)	{
	$querys[] =  "DELETE FROM #__users WHERE id='".$IDJ."';";
	$querys[] = "DELETE FROM #__user_usergroup_map WHERE user_id='".$IDJ."';";
	$querys[] = "DELETE FROM amap_adherentpermission WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_abonnementadherent WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_adherent WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_adhesion WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_distribution WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_livraison WHERE IDA='".$IDA."';";
	$querys[] = "DELETE FROM amap_orphelin WHERE IDAadoptant='".$IDA."';";
	foreach ($querys as $query)	{
		$db=&JFactory::getDBO();
		$db->setQuery($query);
		$db->query($query);
	}
}

function gestusers_AjouterMembre ($IDJ, $IdGroup)	{
	$query = "INSERT INTO #__user_usergroup_map     VALUE ('".$IDJ."', '".$IdGroup."')";
	$db=&JFactory::getDBO();
	$db->setQuery($query);
	$db->query($query);
}

function gestusers_SupprimerMembre ($IDJ, $IdGroup)	{
	$query = "DELETE FROM #__user_usergroup_map     WHERE user_id = '".$IDJ."' AND group_id = '".$IdGroup."'";
	$db=&JFactory::getDBO();
	$db->setQuery($query);
	$db->query($query);
}

function gestusers_AjouterMembreComite ($IDJ, $IDA, $IdGroup)	{
	gestusers_AjouterMembre ($IDJ, $IdGroup);
	$query = "INSERT INTO amap_adherentpermission     VALUE ('".$IDA."', 'BUR',1,1)";
	//on rajoute ou retire d'office aux membres du comité la valeur 'BUR' (membre du Bureau au sens gamap)  et les droits de gestion des adherents et des contrats dans la table amap_adherentpermission
	$db=&JFactory::getDBO();
	$db->setQuery($query);
	$db->query($query);
}

function gestusers_SupprimerMembreComite ($IDJ, $IDA, $IdGroup)	{
	gestusers_SupprimerMembre ($IDJ, $IdGroup);
	$query = "DELETE FROM amap_adherentpermission      WHERE IDA = '".$IDA."'";
	//on rajoute ou retire d'office aux membres du comité la valeur 'BUR' (membre du Bureau au sens gamap)  et les droits de gestion des adherents et des contrats dans la table amap_adherentpermission
	$db=&JFactory::getDBO();
	$db->setQuery($query);
	$db->query($query);
}

function gestusers_AjouterMembreAdherent ($IDJ, $IdGroup)	{
	gestusers_AjouterMembre ($IDJ, $IdGroup);
}

function gestusers_SupprimerMembreAdherent ($IDJ, $IdGroup)	{
	gestusers_SupprimerMembre ($IDJ, $IdGroup);
}

//~ Changement de statut d'un membre de l'AMAP
function gestusers_ChangerStatutAdherent ($IDA, $Statut)	{
	$Periode = JRequest::getVar('Periode');
	$db=&JFactory::getDBO();
	$query = "UPDATE amap_adhesion SET Statut = '".$Statut."'      WHERE IDA = '".$IDA."' AND Periode = '".$Periode."'";
	$db->setQuery($query);
	$db->query($query);
}


?>
